// SlagCar/Index/Index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [],
    name: '',
    runningCount: 0,
    todayCount: 0,
    monthCount: 0
  },
  navigator (e) {
    let { opt } = e.currentTarget.dataset;
    if (opt.url === '/SlagCar/Login/Login') {
      let rlatKey = ['refresh', 'token', 'company_name', 'company_id', 'user_name', 'user_id', 'role_id', 'is_platfrom', 'is_agent', 'userLevel', 'openid', 'user_type', 'driver_row_id', 'driver_id', 'driver_name', 'driver_car_row_id', 'driver_car_id', 'driver_car_name']
      rlatKey.forEach(key => wx.removeStorageSync(key));

      wx.reLaunch({
        url: opt.url
      })
      return
    }
    if (opt.url === '/pages/home/home') {
      wx.redirectTo({ url: opt.url });
      return
    }
    wx.navigateTo({
      url: opt.url
    })
  },

  //获取顶部统计
  getOrderCount () {
    let url1 = 'ZTAuth/muck_waybill_running',
      url2 = 'ZTAuth/muck_waybill_today',
      url3 = 'ZTAuth/muck_waybill_this_month',
      { userType } = this.data,
      post = { "is_all": userType !== 'driver', only_my_suoshuchedui: userType === 'carTeam', only_my_destination: userType === 'disposal' }
    return Promise.all([wx.$post(5, url1, post), wx.$post(5, url2, post), wx.$post(5, url3, post)]).then(res => {
      console.log(res);
      let count = [];
      res.forEach((item, i) => {
        let total = 0
        if (item.errcode == 0) {
          total = item.data.row && item.data.row[0].total || 0;
        }
        count[i] = total;
      });
      this.setData({
        runningCount: count[0],
        todayCount: count[1],
        monthCount: count[2],
      })
    })

  },
  navTo (e) {
    let { url } = e.currentTarget.dataset;
    wx.navigateTo({ url })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    wx.$setBarTitle();
    let { orderid, redirect } = options;
    wx.hideHomeButton({});
    let userType = wx.getStorageSync('user_type'),
      name = '';
    if (userType === 'driver') {
      name = wx.getStorageSync('driver_name')
      let car = wx.getStorageSync('driver_car_name') || '';
      car && (name = name + '-' + car);
    } else {
      name = wx.getStorageSync('user_name')
    }
    let navList = [
      // {title: '开始运输', icon: 'logistics',style:'#99cc33',url: '/SlagCar/Transit/Transit'},
      { title: '开始运输', icon: 'logistics', style: '#99cc33', url: '/SlagCar/DriverTransit/DriverTransit' },
      { title: '渣土清运', icon: 'delete-o', style: '#1ba838', url: '/SlagCar/GrabOrdersList/GrabOrdersList' },
      { title: '我的任务', icon: 'notes-o', style: '#1ba838', url: '/SlagCar/WaybillList/WaybillList' },
      { title: '车辆一日三检', icon: 'completed', style: '#1ba838', url: '/SlagCar/DailyCheckNew/DailyCheckNew' },
      { title: '车辆周检', icon: 'completed', style: '#1ba838', url: '/SlagCar/DailyCheck/DailyCheck' },
      { title: 'OA审批', icon: 'records', style: '#1ba838', url: '/SlagCar/OA/OA' },
      { title: '返回', icon: 'revoke', style: '#1ba838', url: '/pages/home/home' },
    ]
    if (userType === 'system') {
      navList = [
        { title: '渣土清运', icon: 'delete-o', style: '#1ba838', url: '/SlagCar/GrabOrdersList/GrabOrdersList' },
        { title: '订单管理', icon: 'notes-o', style: '#99cc33', url: '/SlagCar/OrderList/OrderList' },
        { title: '运输管理', icon: 'logistics', style: '#1ba838', url: '/SlagCar/WaybillList/WaybillList' },
        { title: '基础资料', icon: 'wap-home-o', style: '#1ba838', url: '/SlagCar/Navigator/Navigator' },
        // {title: '日检记录', icon: 'records',style:'#1ba838',url: '/SlagCar/DailyCheckList/DailyCheckList?type=admin'},
        { title: '日检记录', icon: 'records', style: '#1ba838', url: '/SlagCar/DailyCheckNewStatistics/DailyCheckNewStatistics' },
        { title: '周检记录', icon: 'records', style: '#1ba838', url: '/SlagCar/DailyCheckStatistics/DailyCheckStatistics' },
        // {title: '工地管理', icon: 'wap-home-o',style:'#1ba838',url: '/DIYPages/general_entity/general_entity?entity=all_delivery'},
        // {title: '消纳场管理', icon: 'home-o',style:'#1ba838',url: '/DIYPages/general_entity/general_entity?entity=all_destination'},
        { title: '订单统计', icon: 'chart-trending-o', style: '#1ba838', url: '/SlagCar/OrderStatistics/OrderStatistics' },
        { title: '统计', icon: 'chart-trending-o', style: '#1ba838', url: '/SlagCar/Statistics/Statistics' },
        { title: '司机注册', icon: 'user-o', style: '#1ba838', url: '/SlagCar/DriverRegisterList/DriverRegisterList' },
        { title: '返回', icon: 'revoke', style: '#1ba838', url: '/pages/home/home' },
      ]
    }
    // 外部车队
    if (userType === 'carTeam') {
      navList = [
        { title: '渣土清运', icon: 'delete-o', style: '#1ba838', url: '/SlagCar/GrabOrdersList/GrabOrdersList' },
        { title: '运输管理', icon: 'logistics', style: '#1ba838', url: '/SlagCar/WaybillList/WaybillList' },
        { title: '统计', icon: 'chart-trending-o', style: '#1ba838', url: '/SlagCar/Statistics/Statistics' },
        { title: '返回', icon: 'revoke', style: '#1ba838', url: '/pages/home/home' },
      ]
    }
    // 消纳场
    if (userType === 'disposal') {
      navList = [
        { title: '渣土清运', icon: 'delete-o', style: '#1ba838', url: '/SlagCar/GrabOrdersList/GrabOrdersList' },
        { title: '运输管理', icon: 'logistics', style: '#1ba838', url: '/SlagCar/WaybillList/WaybillList' },
        { title: '返回', icon: 'revoke', style: '#1ba838', url: '/pages/home/home' },
      ]
    }

    this.setData({ name, navList, userType });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    let loginType = wx.getStorageSync('loginType'),
      navList = [];
    this.getOrderCount();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})
